//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data.Common;
using System.Data;

using Microsoft.Practices.EnterpriseLibrary.Data;

using Shp.Runtime.Contract;
using Shp.Runtime.Services.Properties;

namespace Shp.Runtime.Services
{
    static public class WorkflowLogic
    {
        static public void AddWorkflowDef(Guid tenantId, string name, string xoml)
        {
            Database db = DatabaseFactory.CreateDatabase(Constants.DB.TenantMetadataStore);
            using (DbCommand command = db.GetStoredProcCommand("dbo.AddWorkflowDef"))
            {
                db.AddInParameter(command, "tenantId", DbType.Guid, tenantId);
                db.AddInParameter(command, "name", DbType.String, name);
                db.AddInParameter(command, "xoml", DbType.String, xoml);
                db.ExecuteNonQuery(command);
            }
        }

        static public void UpdateWorkflowDef(Guid tenantId, Guid workflowId, string name, string xoml)
        {
            Database db = DatabaseFactory.CreateDatabase(Constants.DB.TenantMetadataStore);
            using (DbCommand command = db.GetStoredProcCommand("dbo.UpdateWorkflowDef"))
            {
                db.AddInParameter(command, "workflowId", DbType.Guid, workflowId);
                db.AddInParameter(command, "tenantId", DbType.Guid, tenantId);
                db.AddInParameter(command, "name", DbType.String, name);
                db.AddInParameter(command, "xoml", DbType.String, xoml);
                db.ExecuteNonQuery(command);
            }
        }

        static public WorkflowDto GetWorkflowDef(Guid workflowDefId)
        {
            WorkflowDto dto;
            Database db = DatabaseFactory.CreateDatabase(Constants.DB.TenantMetadataStore);
            using (DbCommand command = db.GetStoredProcCommand("dbo.GetWorkflowDef"))
            {
                db.AddInParameter(command, "workflowDefId", DbType.Guid, workflowDefId);
                using (IDataReader reader = db.ExecuteReader(command))
                {
                    while (reader.Read())
                    {
                        dto = new WorkflowDto();
                        dto.Id = reader.GetGuid(0);
                        dto.TenantId = reader.GetGuid(1);
                        dto.Name = reader.GetString(2);
                        dto.Xoml = reader.GetString(3);
                        return dto;
                    }
                }
            }
            throw new ArgumentException("workflowDefId");
        }

        static public Dictionary<Guid, string> GetWorkflowDefList(Guid tenantId)
        {
            Database db = DatabaseFactory.CreateDatabase(Constants.DB.TenantMetadataStore);
            Dictionary<Guid, string> list = new Dictionary<Guid, string>(); ;
            // we add the options dynamically
            using (DbCommand command = db.GetStoredProcCommand("dbo.GetWorkflowDefList"))
            {
                db.AddInParameter(command, "tenantId", DbType.Guid, tenantId);
                using (IDataReader reader = db.ExecuteReader(command))
                {
                    while (reader.Read())
                    {
                        if (list.ContainsKey(reader.GetGuid(0)))
                        {
                            // An incosistent menu definition has been added directly to the db
                            // you should guard against this behaviour
                            throw new System.Security.SecurityException(Resources.InvalidConfigurationDuplicateKey);
                        }
                        else
                        {
                            list.Add(reader.GetGuid(0), reader.GetString(1));
                        }
                    }
                }
            }

            return list;
        }

        static public void UpdateRuleSetDef(Guid tenantId, string markup)
        {
            Database db = DatabaseFactory.CreateDatabase(Constants.DB.TenantMetadataStore);
            using (DbCommand command = db.GetStoredProcCommand("dbo.UpdateRuleSetDef"))
            {
                db.AddInParameter(command, "tenantId", DbType.Guid, tenantId);
                db.AddInParameter(command, "markup", DbType.String, markup);
                db.ExecuteNonQuery(command);
            }
        }

        static public string GetRuleSetDef(Guid tenantId)
        {
            Database db = DatabaseFactory.CreateDatabase(Constants.DB.TenantMetadataStore);
            using (DbCommand command = db.GetStoredProcCommand("dbo.GetRuleSetDef"))
            {
                db.AddInParameter(command, "tenantId", DbType.Guid, tenantId);
                string xaml = (string)db.ExecuteScalar(command);
                return xaml;
            }
        }
    }
}